package com.samsung.sds.test.editors;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WhitespaceRule;
import org.eclipse.jface.text.rules.WordRule;

public class JAVASCRIPTScanner extends RuleBasedScanner {
	private static final String[] keywords = JAVASCRIPTPartitionScanner.keywordTokens;
	private static final String[] constants = JAVASCRIPTPartitionScanner.constantTokens;

	public JAVASCRIPTScanner(ColorManager manager) {
		final IToken keyword =
				new Token(
						new TextAttribute(manager.getColor
								(IJAVASCRIPTColorConstants.KEYWORD)));
		final IToken constant =
				new Token(
						new TextAttribute(manager.getColor
								(IJAVASCRIPTColorConstants.KEYWORD)));
		final IToken string =
				new Token(
						new TextAttribute(manager.getColor
								(IJAVASCRIPTColorConstants.STRING)));
		final IToken comment =
				new Token(
						new TextAttribute(
								manager.getColor(IJAVASCRIPTColorConstants.COMMENT)));
		final IToken function =
				new Token(
						new TextAttribute(manager.getColor
								(IJAVASCRIPTColorConstants.KEYWORD)));
		final IToken other =
				new Token(
						new TextAttribute(manager.getColor
								(IJAVASCRIPTColorConstants.DEFAULT)));

		final List rules = new ArrayList();
		rules.add(new EndOfLineRule("//", comment)); 
		rules.add(new MultiLineRule("/*", "*/", comment, (char) 0, true));
		rules.add(new SingleLineRule("\"", "\"", string, '\\'));
		rules.add(new SingleLineRule("'", "'", string, '\\'));
		rules.add(new WhitespaceRule(new JAVASCRIPTWhitespaceDetector()));

		final WordRule wordRule = new WordRule(new JAVASCRIPTWordDetector(), other);
		wordRule.addWord("script", constant); 
		wordRule.addWord("SCRIPT", constant); 
	
		for (int i = 0; i < keywords.length; i++)
			wordRule.addWord(keywords[i], keyword);
		for (int i = 0; i < constants.length; i++)
			wordRule.addWord(constants[i], constant);
		rules.add(wordRule);
		final IRule[] result = new IRule[rules.size()];
		rules.toArray(result);
		setRules(result);
	}
}
